<?php

class Follow_AdminController extends Ac_AdminController
{
    /*
    public function dashboardAction()
    {
    }

    public function menuAction()
    {
        $this->view->index = $this->_getParam('index');
    }

    public function indexAction() 
    {
        $this->listAction();
    }
     */    
    public function listAction() 
    {
        $per_page                   = 100;
        $page                       = !empty($_GET['page']) ? $_GET['page'] : 1;
        $this->view->per_page       = $per_page;
        $this->view->listing_number = Model_Testimonial::all()->count();
        $this->view->page           = $page;

        if ($page > 1) {
            $skip_page = ($_GET['page']-1) * $per_page;
        }
        
        if ($page>1) {
            $this->view->list           = Model_Testimonial::all()->skip(($_GET['page']-1) * $per_page)->limit($per_page)->sort(array('c_dt' => -1));
        }
        else {
            $this->view->list           = Model_Testimonial::all()->limit($per_page)->sort(array('c_dt' => -1));
        }
        $this->view->listing_number = $this->view->list->count();
        
        Model_Testimonial::getSignuppage();
        
        /*foreach(Model_Testimonial::getSignuppage() as $testi){
			var_dump($testi->name);
		}*/
        
        
        $this->render('list');
    }
    
    public function addAction() 
    {
        if (!empty($_GET['id'])) {
            
        } 
    }
    
    public function editAction()
    {
        $name         = $_POST['name_en'];
        $message      = $_POST['message_en'];
        $tagline      = $_POST['tagline_en'];
        $name_tc      = $_POST['name_tc'];
        $message_tc   = $_POST['message_tc'];
        $tagline_tc   = $_POST['tagline_tc'];
        $companyname  = $_POST['companyname'];
        $email     = $_POST['email'];
        //$image        = $_POST['image'];
        //$logo         = $_POST['logo'];
        
        //echo '<pre>';
       // print_r($_FILES);
        //exit;
        $image = $logo = '';
        $uploads_dir = PUBLIC_PATH.'/images/photo/original/images/testimonial';
        foreach ($_FILES as $key => $file) {
            if ($file["error"] == UPLOAD_ERR_OK) {
                $tmp_name = $file["tmp_name"];
                $img_name     = $file["name"];
                //var_dump($tmp_name, $uploads_dir.'/'.$name);
                move_uploaded_file($tmp_name, $uploads_dir.'/'.$img_name);
                if ($key == 'image') {
                    $image = '/images/testimonial/'.$img_name;
                } elseif ($key == 'logo') {
                    $logo = '/images/testimonial/'.$img_name;
                }
            }
        }      
        //var_dump($_POST);
        //exit;
        $user = Model_User::one(array('em'=>$email));
        
        $obj          	= new Model_Testimonial();
        $obj->name    	= $name;
        $obj->message 	= $message;
        $obj->tagline 	= $tagline;
        $obj->name_tc    = $name_tc;
        $obj->message_tc = $message_tc;
        $obj->tagline_tc = $tagline_tc;
        $obj->user_id 	= $user->_id;
        $obj->company_name = $companyname;
        $obj->image   	= $image;
        $obj->logo    	= $logo;
        $obj->save();

        $this->listAction();
    }
    
    public function deleteAction()
    {
        $key    = $_GET['id'];
        Model_Testimonial::remove(array('_id'=>new MongoId($key)));
        $this->_redirect($_GET['refer']);
    }    
}
